/* stdio_utils.c - utilites for standard i/o
 *
 */
#include <stdio.h>
#include <stdlib.h>

#include "stdio_utils.h"

struct input_data *
read_stdin(void)
{
	char ch;        /* character from stdin */
	size_t buf = sizeof(char) * 2;

	struct input_data *id;

	if(ferror(stdin)) {
		printf("stdio_utils.c: Error reading file.\n'");
		exit(1);
	} else {
		id = (InputData *) malloc (sizeof(InputData));
		if(id == NULL) {
			printf("stdio_utils.c: Error allocating memory.\n");
			exit(1);
		}
		id->data_size = 0;
		id->data = (char *) malloc((buf + 1) * sizeof(char));
		if(id->data == NULL) {
			printf("stdio_utils.c: Error allocating memory.\n");
			exit(1);
		}

		ch = fgetc(stdin);

		id->data[id->data_size] = ch;

		do {
			id->data_size++;
			if(id->data_size > buf) {
				id->data = (char *) realloc(id->data,
				                            (id->data_size + 1) * 
				                            sizeof(char *));
				if (id->data == NULL) {
					fprintf(stderr,
					        "stdio_utils.c: error reallocating inputarr.");
				}
			}

			ch = fgetc(stdin);
			id->data[id->data_size] = ch;
		} while(ch != EOF);

		id->data[id->data_size] = '\0';
	}

	return id;
}
